<?php
class SemestersController extends AppController {

	var $name = 'Semesters';
	var $helpers = array('Html', 'Form' );
	var $uses = array("Semester", "Term", "Apartment", "Assignment");

	function admin_index() {
		$this->Semester->recursive = 0;
		$this->set('semesters', $this->Semester->findAll());
	}

	function admin_add() {
		$this->set('termList', $this->Term->getTermList());
		if (empty($this->data)) {
			$this->render();
		} else {
			$this->cleanUpFields();
			if (isset($this->data['Semester']['semester_id'])) {
				$this->admin_copy();
				return;
			}
			if ($this->Semester->save($this->data)) {
				$this->Session->setFlash('The Semester has been saved');
				$this->redirect('/admin/semesters/');
			} else {
				$this->Session->setFlash('Please correct errors below.');
			}
		}
	}

	function admin_edit($id = null) {
		$this->set('termList', $this->Term->getTermList());
		if (empty($this->data)) {
			if (!$id) {
				$this->Session->setFlash('Invalid id for Semester');
				$this->redirect('/admin/semestsers');
			}
			$this->data = $this->Semester->read(null, $id);
		} else {
			$this->cleanUpFields();
			if ($this->Semester->save($this->data)) {
				$this->Session->setFlash('The Semester has been saved');
				$this->redirect('/admin/semesters');
			} else {
				$this->Session->setFlash('Please correct errors below.');
			}
		}
	}

	function admin_copy() {
		$this->set('termList', $this->Term->getTermList());
		$this->set('semesterList', $this->Semester->getSemesterList());
		if (empty($this->data)) {
			$this->render();
			return;
		}

		$oldSemesterId = $this->data['Semester']['semester_id'];

		/*save new one*/
		$this->data['Semester']['semester_id'] = null;
		$this->Semester->save($this->data);
		$semesterId = $this->Semester->getLastInsertId();

		/*copy the apartments*/
		$apartments = $this->Apartment->findAllBySemesterId($oldSemesterId);
		foreach($apartments as $apartment) {
			print_r($apartment);
			$apartmentId = $apartment['Apartment']['apartment_id'];
			$apartment['Apartment']['semester_id'] = $semesterId;
			$apartment['Apartment']['apartment_id'] = null;
			$this->Apartment->save($apartment);
			$newApartmentId = $this->Apartment->getLastInsertId();
			$assignments = $this->Assignment->findAllByApartmentId($apartmentId);
			foreach ($assignments as $assignment) {
				$assignment['Assignment']['apartment_id'] = $newApartmentId;
				$assignment['Assignment']['assignment_id'] = null;
				$this->Assignment->save($assignment);
			}
		}

		$this->Session->setFlash('Semester copied');
		$this->redirect('/admin/semesters/');
	}

	function admin_delete($semesterId = null) {
		if ($semesterId == null) {
			$this->redirect('/admin/semesters');
			exit();
		}

		$this->Semester->del($semesterId);
		$apartments = $this->Apartment->findAllBySemesterId($semesterId);
		foreach($apartments as $apartment) {
			$apartmentId = $apartment['Apartment']['apartment_id'];
			$this->Apartment->del($apartmentId);

			$assignments = $this->Assignment->findAllByApartmentId($apartmentId);
			foreach ($assignments as $assignment) {
				$assignmentId = $assignment['Assignment']['assignment_id'];
				$this->Assignment->del($assignmentId);
			}
		}
		$this->redirect('/admin/semesters');
	}
}
?>
